$(function(){
    var c_city=Utils.getCookie("currentCity");//获取cookie中的地址信息
    var l_city=Utils.getCurrentCity("currentCity");//获取当前的城市地址
    var curr_city=document.getElementById("currentCity");//找到currentCity对象
    var shop_u_id=Utils.getQueryString("shopId");//通过url
    var shop_c_id=Utils.getCookie("shopId");//通过cookie
    var user_u_id=Utils.getQueryString("userId");//通过url
    var user_c_id=Utils.getCookie("userId");//通过cookie
    var productId=Utils.getQueryString("productId");//当前产品名
    var userId=0;
    if(user_c_id||user_u_id){
        userId=parseInt(user_c_id||user_u_id);
    }
    var hasLogin=Utils.getCookie("hasLogin");//用户已登录
    var url=Utils.reqUrl,
        asyn=true,
        type="post";
    function getCurr_city(){
        var  reqUrl='http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js';
        function callback(){
            var city=remote_ip_info.city;
            Utils.setCookie("currentCity",city);
            curr_city.innerHTML=city;
        }
        //如果已经定位过,直接设置为已定位的，否则重新自动定位
        if(l_city||c_city){
            var city=l_city||c_city;
            Utils.setCookie("currentCity",city);
            curr_city.innerHTML=city;    
        }else{
            $.getScript(reqUrl,callback);
        }
        setIndexData(curr_city.innerHTML);
        $(".changecity a").on("click",function(){
            location.href="./address.html?currentCity="+curr_city.innerHTML;
        });
    }
    //定位城市
    getCurr_city(setIndexData);
    function setIndexData(current_city){
        var address;
        if(c_city||l_city){
            address=c_city||l_city;
        }else{
            address=current_city;
        }
        /**
         *----页面跳转
         **/
        function jump_page(ele){
            var ele=ele;
            $(ele).on("click",function(){
                var links=$(this).attr("data-links");
                location.href="./"+links+".html?currentCity="+address;
            });   
        }
        jump_page(".jump_links");
        
        //点击服务分类，加载服务类别
        $(".nav_down").on("click",function(){
        if($(".service_nav").css("display")=="none"){
            $(".service_nav").css("display","block");
            $(".classifynav .nav_down i").addClass("bgup").removeClass("bgdown");
        }else{
            $(".service_nav").css("display","none");
            $(".classifynav .nav_down i").addClass("bgdown").removeClass("bgup");
        }
        console.log("asdas");
           //获取服务大类
        var requestData1=JSON.stringify(
            {
                "code":"getPdOrServTypeList",
                "version":"1.0",
                "data":
                {
                    "parentServiceId":0,
                    "type":"SERVICE",
                    "parentPdTypeId":0
                }
            });
        getData(url,requestData1,call_func1,asyn,type);
        function call_func1(data){
            console.log(data);
            var serviceTypes=data.data.serviceTypes;
            var serviceTypesLen=serviceTypes.length;
            var typeStr='';
            for(var i=0;i<serviceTypesLen;i++){
                typeStr+='<li class="service_nav_li l" data-serviceId="'+serviceTypes[i].serviceId+'">'+serviceTypes[i].serviceName+'</li>';
            }
            $(".service_nav").html(typeStr);
            $(".service_nav li").on("mouseover",function(e){
                serId=parseInt($(this).attr("data-serviceId"));

                //获取服务xiao类
                var requestData2=JSON.stringify({
                    "code":"getPdOrServTypeList",
                    "version":"1.0",
                    "data":{
                        "parentServiceId":serId,
                        "type":"SERVICE",
                        "parentPdTypeId":0
                    }
                });
                getData(url,requestData2,call_func2,asyn,type);
            });
        }
        function call_func2(data){
            console.log(data);
            var serviceTypesM=data.data.serviceTypes;
            var serviceTypesMLen=serviceTypesM.length;
            var li3='';
            for(var i=0;i<serviceTypesMLen;i++){
                li3+='<li class="nav_kid_li l" data-serviceIds="'+serviceTypesM[i].serviceId+'">'+serviceTypesM[i].serviceName+'</li>';
            }
            $(".nav_kid").html(li3);
        }
        setAction(".service_nav",".service_nav_kid");//服务
    });
        
    //类型菜单选择样式
        function setAction(ele,_ele){
        var ele=ele;
        var _ele=_ele;
        $(ele).on({//大类鼠标停留跟移除
            mouseover:function(){
                console.log("as");
                show_action();
            },
            mouseout:function(){
                hide_action();    
            }
        });
        $(_ele).find(".nav_kid").on({//小类鼠标停留跟移除
            mouseover:function(){
                show_action();
            },
            mouseout:function(){
                hide_action();
            }
        });
        function show_action(){
            $(_ele).addClass("show").removeClass("hide");
        }
        function hide_action(){
            $(_ele).addClass("hide").removeClass("show");
        }
        $(document).on("click",_ele+" ul li",function(){//选择小类并且隐藏，跳转到指定的页面
            $(_ele).addClass("hide").removeClass("show");
            $(ele).css("display","none");
            var serviceId=$(this).attr("data-serviceIds");
               var zwl_flag=1;
               location.href = "./service.html?zwl_flag="+zwl_flag+"&serviceId="+serviceId+"&parentServiceId="+serId;
        }); 
    }

        
        var serId;//大类id
        function getData(url,area_data,call_func,asyn,type){
            console.log("请求参数"+area_data);
            $.ajax({
                url:url,
                type:type,
                datatype:"json",
                contentType:"application/json",
                data:area_data,
                async:asyn,
                success:function(data){
                    console.log(data);
                    call_func(data);  
                },
                error:function(msg){
                        console.log(msg);
                }   
            });        
        }
         var requestData=JSON.stringify(
            {
                "code":"getServiceDetail",
                "version":"1.0",
                "data":{
                    "productId":productId,
                    "userId":userId
                }
            });
        getData(url,requestData,call_func_ser_detail,asyn,type);
        function call_func_ser_detail(data){
            var serviceData=data.data.serviceInfo;
            var parentServiceName=serviceData.parentServiceName;
            var serviceName=serviceData.serviceName;
            var productName=serviceData.productName;
            var remark=serviceData.remark;
            var saleprice=serviceData.saleprice;
            var unit=serviceData.unit;
            var phoneNO=serviceData.phoneNO;
            var viewCount=serviceData.viewCount;
            var serviceCollectCount=serviceData.serviceCollectCount;
            var isCollect=serviceData.isCollect;//判断用户是否收藏  0是为收藏  1是收藏
            //已收藏标志
            if(isCollect==="1"){
                $(".collect-log .img1").addClass("has-collect").removeClass("un-collect");
                $(".collect-log .count1").html("已收藏");
            }
            $(".name-p").html(productName);
            $(".intro-detail").html(remark);
            $(".price-num").html("￥"+saleprice);
            $(".unit").html("("+unit+")");
            $(".contact-phone").html(phoneNO);
            $(".collect-sc .count").html("收藏量："+serviceCollectCount);
            $(".collect-ll .count").html("浏览量："+viewCount);
            $(".serviceDetail-intro").html(remark);
            $(".level_1").html(parentServiceName);
            $(".level_2").html(serviceName);
            var userImgPath=serviceData.userImgPath;
            var fullpath=userImgPath.add_suffix_max();//替换成后缀为_max的图片
            var serviceImgs=serviceData.serviceImgs;
            var serviceImgsLen=serviceImgs.length;
            var li='';
            var li2='';
            if(serviceImgsLen<1){
                $(".jqzoom").html('<IMG height=400 src="'+fullpath+'" jqimg="'+fullpath+'" width=400>');
                li+=' <li><img src="'+fullpath+'"> </li>';
                $(".serviceDetail-img").html('<img src="'+fullpath+'" alt="">');
               
            }else{
                $(".jqzoom").html('<IMG height=400 src="'+serviceImgs[0].add_suffix_max()+'" jqimg="'+serviceImgs[0].add_suffix_max()+'" width=400>');
                for(var i=0;i<serviceImgsLen;i++){
                    li+=' <li><img src="'+serviceImgs[i].add_suffix_max()+'"> </li>';
                    li2+='<img src="'+serviceImgs[i].add_suffix_max()+'" alt="">';
                }
                $(".serviceDetail-img").html(li2);
            }
            $(".list-h").html(li);
            setCollect();
            //进入页面后判断是否登录，
            function setCollect(){
                if(!hasLogin){//未登录(默认显示未收藏的标志)
                    $(".collect-log").find(".img1").addClass("un-collect").removeClass("has-collect");
                }
                clickCollect();
            }
           
            function clickCollect(){
                $(".collect-log").on("click",function(){
                    if(hasLogin){
                        console.log("asdkj");
                        var coll_img=$(this).find(".img1").attr("class");
                        if(coll_img.indexOf("un-collect")>-1){//收藏
                            $(this).find(".img1").addClass("has-collect").removeClass("un-collect");
                            $(".collect-log .count1").html("已收藏");
                            collectData=JSON.stringify({
                                "code":"serviceCollect",
                                "data":{
                                 "productId":productId,
                                 "userId":userId
                                },
                                "version":"1.0"
                            });
 getData(url,collectData,call_func_coll_result,asyn,type);
                        }else{//取消收藏
                            $(this).find(".img1").addClass("un-collect").removeClass("has-collect");
                            $(".collect-log .count1").html("收藏");
                            cancelData=JSON.stringify({
                                "code":"cancelCollect",
                                "data":{
                                    "productIds":[productId],
                                    "userId":userId
                                },
                                "version":"1.0"
                            });
                            getData(url,cancelData,call_func_coll_result,asyn,type);
                        }
                    }else{
                        $(".login").show(200);
                        $("#loginBox").show(500);
                        var phoneNO;
                        var requestData="";
                        var userNo= Utils.getCookie("userNo");
                        var pwd= Utils.getCookie("password");
                        if(pwd){
                            $("[name='checkbox']").attr("checked", "true");
                        }
                        if (userNo) {//用户名存在的话就把用户名填充到用户名文本框
                            $(".login-name .login-input").val(userNo);
                        }
                        if (pwd) {//密码存在的话把密码填充到密码文本框
                            $(".login-pwd .login-input").val(pwd);
                        }
                        var password=$(".login-pwd .login-input").val();

                        //手动输入加密
                        $(".login-pwd .login-input").change(function(){
                              password=$(".login-pwd .login-input").val();
                              password=hex_md5(password);
                        });
                        $(".loginIn").on("click",function(){
                            phoneNO=$(".login-name .login-input").val();
                            var p_wd=$(".login-pwd .login-input").val();
                             if($.trim(p_wd)=="" || $.trim(phoneNO)==""){
                                  $(".error").css("display","block");
                            }else{
                                   $(".error").css("display","none");
                            }
                            logindata=JSON.stringify({
                                "code":"login",
                                "version":"1.0",
                                "data":{
                                    "phoneNO":phoneNO,
                                    "password":password
                                }
                            });
                            getData(url,logindata,handdle_func1,asyn,type);
                        });            
                        function handdle_func1(data){
                            var userName=data.data.user.userName;//用户名
                            var userId=data.data.user.userId;//用户id
                            var shopId=data.data.seller.shopId;//商家id
                            if(data.msg=="登录成功"){
                                console.log("登录成功");
                                hasLogin=true;//登录成功
                                Utils.setCookie("shopId",shopId);
                                Utils.setCookie("hasLogin",hasLogin);//已登录标志
                                Utils.setCookie("userName",userName);//用户名
                                Utils.setCookie("userId",userId);//用户id
                                if($(".auto-login").attr("checked")){//记住密码
                                    Utils.setCookie("userNo",phoneNO);
                                    Utils.setCookie("password",password);//登录成功

                                }else{
                                    Utils.delCookie("userNo");
                                    Utils.delCookie("password");

                                }
                                //登录成功之后，reload当前页面或者跳转到当前的页面
                                location.reload();
                            }else{
                                console.log("登录失败");
                                $(".error").css("display","block");
                                $(".error").html("账号或者密码错误！");
                            }
                        }    
                    }
                });
            }
             function call_func_coll_result(data){
                console.log("收藏取消结果");
            }
        } 
        //关闭登陆窗口    
        $(document).on("click",".closeBtn",function(){
            $(".login").hide(200);
            $("#loginBox").hide(500);
        });
    }
	});